package net.jlibrary.curbparking.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import net.jlibrary.account.domain.Employee;
import net.jlibrary.common.core.conversion.JsonUtil;
import net.jlibrary.common.core.service.BaseManager;
import net.jlibrary.common.core.service.IHttpSend;
import net.jlibrary.common.exception.ExceptionUtil;
import net.jlibrary.common.project.domain.Group;
import net.jlibrary.common.project.domain.PermissionFunction;
import net.jlibrary.common.project.domain.PermissionRole;
import net.jlibrary.common.project.domain.PermissionRoleType;
import net.jlibrary.common.project.domain.PermissionUrl;
import net.jlibrary.common.project.service.IDictionaryManager;
import net.jlibrary.common.tools.DateUtil;
import net.jlibrary.common.tools.MD5Encrypt;
import net.jlibrary.common.tools.ParamsProcessing;
import net.jlibrary.common.tools.StringUtil;
import net.jlibrary.common.tools.TokenProcessor;
import net.jlibrary.curbparking.Constants;
import net.jlibrary.curbparking.domain.BerthStatus;
import net.jlibrary.curbparking.domain.CarType;
import net.jlibrary.curbparking.domain.DeviceStatus;
import net.jlibrary.curbparking.domain.ParkRecordStatus;
import net.jlibrary.curbparking.domain.PictureGalleryType;
import net.jlibrary.curbparking.domain.PlateColor;
import net.jlibrary.group.domain.Area;
import net.jlibrary.group.domain.GroupHq;
import net.jlibrary.group.domain.Merchant;
import net.jlibrary.group.domain.ParkingPlace;
import net.jlibrary.group.domain.ParkingPlaceType;
import net.jlibrary.vo.PermissionConstants;
import net.jlibrary.vo.curbparking.CurbParkingReq;
import net.jlibrary.vo.curbparking.CurbParkingRes;
import net.jlibrary.vo.curbparking.ServiceConstants;
import net.jlibrary.vo.http.HttpResult;
import net.jlibrary.vo.http.HttpUrlAndParams;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class DataSynchronizedManager extends BaseManager implements IDataSynchronizedManager {
	private final Log log = LogFactory.getLog(getClass());
	@Resource
	private IDictionaryManager dictionaryManager;
	@Resource
	private IHttpSend httpSend;

	@Override
	public void dataInit() {
		try {
			// 权限
			PermissionFunction permissionFunction = new PermissionFunction();
			if (dao.getTotalRow(permissionFunction) == 0) {
				log.info("开始初始化权限");
				PermissionRoleType roleType = new PermissionRoleType();
				
				roleType = new PermissionRoleType();
				roleType.setCode("bk");
				if (dao.getTotalRow(roleType) == 0) {
					roleType.setName("后台");
					roleType.setTaxis(1);
					roleType.setValid(true);
					dao.baseSaveObject(roleType);
				}

				

				permissionFunction = new PermissionFunction();
				PermissionUrl url = new PermissionUrl();
				url.setUrl("/**/*Admin*");
				if (dao.getTotalRow(url) == 0) {
					url.setCreatedate(DateUtil.getCurrentTime());
					url.setFunction(permissionFunction);
					url.setLastdate(DateUtil.getCurrentTime());
					permissionFunction.getUrls().add(url);
					permissionFunction.setCode("bk-base");
					permissionFunction.setName("后台基础功能");
					permissionFunction.setValid(true);
					dao.baseSaveObject(permissionFunction);

					PermissionRole role = new PermissionRole();
					role.setCode(PermissionConstants.PERMISSION_ROLE_BK_MANAGER);
					role.getFunctions().add(permissionFunction);
					role.setName("后台管理员");
					role.setRemoteLogin(true);
					roleType = new PermissionRoleType();
					roleType.setCode("bk");
					role.setType((PermissionRoleType) dao.baseLists(roleType, false).get(0));
					role.setValid(true);
					dao.baseSaveObject(role);
				}

				GroupHq hq = new GroupHq();
				hq.setCode("ddzj");
				hq.setFullName("深圳市大道至简信息技术有限公司");
				hq.setGlobalPath("/ddzj");
				hq.setShortName("大道至简");
				hq.setValid(true);
				dao.baseSaveObject(hq);


				
				log.info("权限初始化完毕");
			}

			// 开始初始化合作商户权限
			PermissionRoleType rt = new PermissionRoleType();
			rt.setCode(PermissionConstants.PERMISSION_ROLE_TYPE_MERCHANT);
			if (dao.getTotalRow(rt) == 0) {
				rt.setName("合作商户");
				rt.setTaxis(5);
				rt.setValid(true);
				dao.baseSaveObject(rt);

				permissionFunction = new PermissionFunction();
				PermissionUrl url = new PermissionUrl();
				url.setUrl("/merchant/*Merchant*");
				if (dao.getTotalRow(url) == 0) {
					url.setCreatedate(DateUtil.getCurrentTime());
					url.setFunction(permissionFunction);
					url.setLastdate(DateUtil.getCurrentTime());
					permissionFunction.getUrls().add(url);
					permissionFunction.setCode("merchant-base");
					permissionFunction.setName("合作商户基础功能");
					permissionFunction.setValid(true);
					dao.baseSaveObject(permissionFunction);

					PermissionRole role = new PermissionRole();
					role.setCode(PermissionConstants.PERMISSION_ROLE_MERCHANT_MANAGER);
					role.getFunctions().add(permissionFunction);
					role.setName("合作商户管理员");
					role.setRemoteLogin(true);
					rt = new PermissionRoleType();
					rt.setCode(PermissionConstants.PERMISSION_ROLE_TYPE_MERCHANT);
					role.setType((PermissionRoleType) dao.baseLists(rt, false).get(0));
					role.setValid(true);
					dao.baseSaveObject(role);
				}
			}

			

			BerthStatus berthStatus = new BerthStatus();
			berthStatus.setCode(Constants.BERTH_STATUS_B001);
			if (dao.isExists(berthStatus) == false) {
				log.debug("初始化泊位状态 -> 占用");
				BerthStatus bs = new BerthStatus();
				bs.setCode(berthStatus.getCode());
				bs.setName("占用");
				berthStatus.setCode(null);
				bs.setTaxis(dao.getTotalRow(berthStatus) + 1);
				bs.setValid(true);
				dao.baseSaveObject(bs);
			}
			berthStatus.setCode(Constants.BERTH_STATUS_B002);
			if (dao.isExists(berthStatus) == false) {
				log.debug("初始化泊位状态 -> 空闲");
				BerthStatus bs = new BerthStatus();
				bs.setCode(berthStatus.getCode());
				bs.setName("空闲");
				berthStatus.setCode(null);
				bs.setTaxis(dao.getTotalRow(berthStatus) + 1);
				bs.setValid(true);
				dao.baseSaveObject(bs);
			}
			berthStatus.setCode(Constants.BERTH_STATUS_B003);
			if (dao.isExists(berthStatus) == false) {
				log.debug("初始化泊位状态 -> 未知");
				BerthStatus bs = new BerthStatus();
				bs.setCode(berthStatus.getCode());
				bs.setName("未知");
				berthStatus.setCode(null);
				bs.setTaxis(dao.getTotalRow(berthStatus) + 1);
				bs.setValid(true);
				dao.baseSaveObject(bs);
			}

			CarType carType = new CarType();
			carType.setCode(Constants.CAR_TYPE_CT000);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 未知");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("未知");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT001);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 轿车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("轿车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT002);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 面包车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("面包车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT003);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 大型客车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("大型客车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT004);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 中型客车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("中型客车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT005);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 皮卡");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("皮卡");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT006);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 非机动车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("非机动车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT007);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> SUV");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("SUV");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT008);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> MPV");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("MPV");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT009);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 微型货车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("微型货车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT010);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 轻型货车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("轻型货车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT011);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 中型货车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("中型货车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			carType.setCode(Constants.CAR_TYPE_CT012);
			if (dao.isExists(carType) == false) {
				log.debug("初始化车型 -> 重型货车");
				CarType ct = new CarType();
				ct.setCode(carType.getCode());
				ct.setName("重型货车");
				carType.setCode(null);
				ct.setTaxis(dao.getTotalRow(carType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}

			PlateColor plateColor = new PlateColor();
			plateColor.setCode(Constants.PLATE_COLOR_PC000);
			if (dao.isExists(plateColor) == false) {
				log.debug("初始化车牌颜色 -> 未知");
				PlateColor ct = new PlateColor();
				ct.setCode(plateColor.getCode());
				ct.setName("未知");
				plateColor.setCode(null);
				ct.setTaxis(dao.getTotalRow(plateColor) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			plateColor.setCode(Constants.PLATE_COLOR_PC001);
			if (dao.isExists(plateColor) == false) {
				log.debug("初始化车牌颜色 -> 蓝底白字");
				PlateColor ct = new PlateColor();
				ct.setCode(plateColor.getCode());
				ct.setName("蓝底白字");
				plateColor.setCode(null);
				ct.setTaxis(dao.getTotalRow(plateColor) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			plateColor.setCode(Constants.PLATE_COLOR_PC002);
			if (dao.isExists(plateColor) == false) {
				log.debug("初始化车牌颜色 -> 黄底黑字");
				PlateColor ct = new PlateColor();
				ct.setCode(plateColor.getCode());
				ct.setName("黄底黑字");
				plateColor.setCode(null);
				ct.setTaxis(dao.getTotalRow(plateColor) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			plateColor.setCode(Constants.PLATE_COLOR_PC003);
			if (dao.isExists(plateColor) == false) {
				log.debug("初始化车牌颜色 -> 白底黑字");
				PlateColor ct = new PlateColor();
				ct.setCode(plateColor.getCode());
				ct.setName("白底黑字");
				plateColor.setCode(null);
				ct.setTaxis(dao.getTotalRow(plateColor) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			plateColor.setCode(Constants.PLATE_COLOR_PC004);
			if (dao.isExists(plateColor) == false) {
				log.debug("初始化车牌颜色 -> 黑底白字");
				PlateColor ct = new PlateColor();
				ct.setCode(plateColor.getCode());
				ct.setName("黑底白字");
				plateColor.setCode(null);
				ct.setTaxis(dao.getTotalRow(plateColor) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			plateColor.setCode(Constants.PLATE_COLOR_PC005);
			if (dao.isExists(plateColor) == false) {
				log.debug("初始化车牌颜色 -> 绿底白字");
				PlateColor ct = new PlateColor();
				ct.setCode(plateColor.getCode());
				ct.setName("绿底白字");
				plateColor.setCode(null);
				ct.setTaxis(dao.getTotalRow(plateColor) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			plateColor.setCode(Constants.PLATE_COLOR_PC006);
			if (dao.isExists(plateColor) == false) {
				log.debug("初始化车牌颜色 -> 绿底黑字");
				PlateColor ct = new PlateColor();
				ct.setCode(plateColor.getCode());
				ct.setName("绿底黑字");
				plateColor.setCode(null);
				ct.setTaxis(dao.getTotalRow(plateColor) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			plateColor.setCode(Constants.PLATE_COLOR_PC007);
			if (dao.isExists(plateColor) == false) {
				log.debug("初始化车牌颜色 -> 绿黄底黑字");
				PlateColor ct = new PlateColor();
				ct.setCode(plateColor.getCode());
				ct.setName("绿黄底黑字");
				plateColor.setCode(null);
				ct.setTaxis(dao.getTotalRow(plateColor) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}

			DeviceStatus deviceStatus = new DeviceStatus();
			deviceStatus.setCode(Constants.DEVICE_STATUS_DS000);
			if (dao.isExists(deviceStatus) == false) {
				log.debug("初始化设备状态 -> 异常");
				DeviceStatus ct = new DeviceStatus();
				ct.setCode(deviceStatus.getCode());
				ct.setName("异常");
				deviceStatus.setCode(null);
				ct.setTaxis(dao.getTotalRow(deviceStatus) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			deviceStatus.setCode(Constants.DEVICE_STATUS_DS001);
			if (dao.isExists(deviceStatus) == false) {
				log.debug("初始化设备状态 -> 正常");
				DeviceStatus ct = new DeviceStatus();
				ct.setCode(deviceStatus.getCode());
				ct.setName("正常");
				deviceStatus.setCode(null);
				ct.setTaxis(dao.getTotalRow(deviceStatus) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			deviceStatus.setCode(Constants.DEVICE_STATUS_DS002);
			if (dao.isExists(deviceStatus) == false) {
				log.debug("初始化设备状态 -> 离线");
				DeviceStatus ct = new DeviceStatus();
				ct.setCode(deviceStatus.getCode());
				ct.setName("离线");
				deviceStatus.setCode(null);
				ct.setTaxis(dao.getTotalRow(deviceStatus) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}

			ParkingPlaceType parkingPlaceType = new ParkingPlaceType();
			parkingPlaceType.setCode(Constants.PARKING_PLACE_TYPE_PPT001);
			if (dao.isExists(parkingPlaceType) == false) {
				log.debug("初始化停车区类型 -> 开放式平行");
				ParkingPlaceType ct = new ParkingPlaceType();
				ct.setCode(parkingPlaceType.getCode());
				ct.setName("开放式平行");
				parkingPlaceType.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkingPlaceType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			parkingPlaceType.setCode(Constants.PARKING_PLACE_TYPE_PPT002);
			if (dao.isExists(parkingPlaceType) == false) {
				log.debug("初始化停车区类型 -> 开放式垂直");
				ParkingPlaceType ct = new ParkingPlaceType();
				ct.setCode(parkingPlaceType.getCode());
				ct.setName("开放式垂直");
				parkingPlaceType.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkingPlaceType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			parkingPlaceType.setCode(Constants.PARKING_PLACE_TYPE_PPT003);
			if (dao.isExists(parkingPlaceType) == false) {
				log.debug("初始化停车区类型 -> 半封闭");
				ParkingPlaceType ct = new ParkingPlaceType();
				ct.setCode(parkingPlaceType.getCode());
				ct.setName("半封闭");
				parkingPlaceType.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkingPlaceType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			parkingPlaceType.setCode(Constants.PARKING_PLACE_TYPE_PPT004);
			if (dao.isExists(parkingPlaceType) == false) {
				log.debug("初始化停车区类型 -> 全封闭");
				ParkingPlaceType ct = new ParkingPlaceType();
				ct.setCode(parkingPlaceType.getCode());
				ct.setName("全封闭");
				parkingPlaceType.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkingPlaceType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			parkingPlaceType.setCode(Constants.PARKING_PLACE_TYPE_PPT005);
			if (dao.isExists(parkingPlaceType) == false) {
				log.debug("初始化停车区类型 -> 其他");
				ParkingPlaceType ct = new ParkingPlaceType();
				ct.setCode(parkingPlaceType.getCode());
				ct.setName("其他");
				parkingPlaceType.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkingPlaceType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}

			ParkRecordStatus parkRecordStatus = new ParkRecordStatus();
			parkRecordStatus.setCode(Constants.PARK_RECORD_STATUS_PRS001);
			if (dao.isExists(parkRecordStatus) == false) {
				log.debug("初始化停车状态 -> 停车中");
				ParkRecordStatus ct = new ParkRecordStatus();
				ct.setCode(parkRecordStatus.getCode());
				ct.setName("停车中");
				parkRecordStatus.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkRecordStatus) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			parkRecordStatus.setCode(Constants.PARK_RECORD_STATUS_PRS002);
			if (dao.isExists(parkRecordStatus) == false) {
				log.debug("初始化停车状态 -> 入场状态不明确");
				ParkRecordStatus ct = new ParkRecordStatus();
				ct.setCode(parkRecordStatus.getCode());
				ct.setName("入场状态不明确");
				parkRecordStatus.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkRecordStatus) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			parkRecordStatus.setCode(Constants.PARK_RECORD_STATUS_PRS003);
			if (dao.isExists(parkRecordStatus) == false) {
				log.debug("初始化停车状态 -> 已离场");
				ParkRecordStatus ct = new ParkRecordStatus();
				ct.setCode(parkRecordStatus.getCode());
				ct.setName("已离场");
				parkRecordStatus.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkRecordStatus) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			parkRecordStatus.setCode(Constants.PARK_RECORD_STATUS_PRS004);
			if (dao.isExists(parkRecordStatus) == false) {
				log.debug("初始化停车状态 -> 离场状态不明确");
				ParkRecordStatus ct = new ParkRecordStatus();
				ct.setCode(parkRecordStatus.getCode());
				ct.setName("离场状态不明确");
				parkRecordStatus.setCode(null);
				ct.setTaxis(dao.getTotalRow(parkRecordStatus) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}

			PictureGalleryType pictureGalleryType = new PictureGalleryType();
			pictureGalleryType.setCode(Constants.PICTURE_GALLERY_TYPE_PGTS001);
			if (dao.isExists(pictureGalleryType) == false) {
				log.debug("初始化图片集类别 -> 入场");
				PictureGalleryType ct = new PictureGalleryType();
				ct.setCode(pictureGalleryType.getCode());
				ct.setName("入场");
				pictureGalleryType.setCode(null);
				ct.setTaxis(dao.getTotalRow(pictureGalleryType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			pictureGalleryType.setCode(Constants.PICTURE_GALLERY_TYPE_PGTS002);
			if (dao.isExists(pictureGalleryType) == false) {
				log.debug("初始化图片集类别 -> 离场");
				PictureGalleryType ct = new PictureGalleryType();
				ct.setCode(pictureGalleryType.getCode());
				ct.setName("离场");
				pictureGalleryType.setCode(null);
				ct.setTaxis(dao.getTotalRow(pictureGalleryType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}
			pictureGalleryType.setCode(Constants.PICTURE_GALLERY_TYPE_PGTS003);
			if (dao.isExists(pictureGalleryType) == false) {
				log.debug("初始化图片集类别 -> 定时抓拍");
				PictureGalleryType ct = new PictureGalleryType();
				ct.setCode(pictureGalleryType.getCode());
				ct.setName("定时抓拍");
				pictureGalleryType.setCode(null);
				ct.setTaxis(dao.getTotalRow(pictureGalleryType) + 1);
				ct.setValid(true);
				dao.baseSaveObject(ct);
			}

			areaListReceive();
			merchantListReceive();
			parkingPlaceListReceive();
			employeeListReceive();
		} catch (Exception e) {
			log.error("载入数据出现异常 ->" + ExceptionUtil.stackTraceToString(e, null));
		} finally {
			// RedisUtil.returnResource(jedis);
		}
	}

	@Override
	public void areaListReceive() {
		if (dao.isExists(new Area()) == false) {
			log.debug("地区列表为空，现在从后台获取地区列表");

			CurbParkingReq req = new CurbParkingReq();
			req.setService(ServiceConstants.CURB_TO_YUN_RECEIVE_AREA_LIST);
			req.setCharset("UTF-8");
			req.setVersion("V1.0");
			req.setSequence(TokenProcessor.getInstance().generateToken());
			req.setTimestamp((System.currentTimeMillis() / 1000) + "");
			req.setSignType("MD5");

			CurbParkingRes res = sendMessage(req);

			if (res != null) {
				List<Map<String, Object>> rs = JsonUtil.toObject(res.getResData(), List.class);
				log.debug("从后台获取地区数量为 " + rs.size());
				for (Map<String, Object> map : rs) {
					String code = (String) map.get("code");
					String name = (String) map.get("name");
					Integer taxis = (Integer) map.get("taxis");
					String parentCode = (String) map.get("parentCode");

					Area area = new Area();
					area.setCode(code);
					area.setName(name);
					area.setTaxis(taxis);
					area.setValid(true);
					if (StringUtil.isNotBlank(parentCode)) {
						Area parentArea = new Area();
						parentArea.setCode(parentCode);
						if (dao.isExists(parentArea) == false) {
							log.warn("没有找到地区编码为 " + code + " 名称为 " + name + " 的上级地区 -> " + parentCode);
							continue;
						}
						parentArea = dictionaryManager.getDictionaryPo(parentCode, Area.class);
						area.setParent(parentArea);
					}
					log.debug("保存地区 " + area.getCode() + "-" + area.getName());
					dao.baseSaveObject(area);
				}
			}
		}
	}

	@Override
	public void merchantListReceive() {
		List<Merchant> ms = dao.baseLists(new Merchant(), false);
		String merchantCodes = "";
		for (Merchant merchant : ms) {
			merchantCodes += merchant.getCode() + ",";
		}
		CurbParkingReq req = new CurbParkingReq();
		req.setService(ServiceConstants.CURB_TO_YUN_RECEIVE_MERCHANT_LIST);
		req.setCharset("UTF-8");
		req.setVersion("V1.0");
		req.setSequence(TokenProcessor.getInstance().generateToken());
		req.setTimestamp((System.currentTimeMillis() / 1000) + "");
		req.setSignType("MD5");

		Map<String, String> maps = new HashMap<String, String>();
		maps.put("merchantCodes", merchantCodes);
		req.setReqData(JsonUtil.toJson(maps));
		CurbParkingRes res = sendMessage(req);
		if (res != null) {
			List<Map<String, Object>> rs = JsonUtil.toObject(res.getResData(), List.class);
			log.debug("从后台获取商户数量为 " + rs.size());
			for (Map<String, Object> map : rs) {
				Merchant merchant = new Merchant();
				merchant.setCode((String) map.get("code"));
				merchant.setEmail((String) map.get("email"));
				merchant.setFax((String) map.get("fax"));
				merchant.setFullName((String) map.get("fullName"));
				merchant.setGlobalPath((String) map.get("globalPath"));
				merchant.setLinkman((String) map.get("linkman"));
				merchant.setMobile((String) map.get("mobile"));
				merchant.setValid(true);
				merchant.setTel((String) map.get("tel"));
				merchant.setShortName((String) map.get("shortName"));
				merchant.setMemo((String) map.get("memo"));
				String parentCode = (String) map.get("parentCode");
				String areaCode = (String) map.get("areaCode");

				if (StringUtil.isNotBlank(parentCode)) {
					Group parent = new Group();
					parent.setCode(parentCode);
					if (dao.isExists(parent) == false) {
						log.warn("没有找到商户编码为 " + merchant.getCode() + " 名称为 " + merchant.getFullName() + " 的上级商户 -> "
								+ parentCode);
						continue;
					}
					parent = (Group) dao.baseLists(parent, false).get(0);
					merchant.setParentGroup(parent);
				}
				if (StringUtil.isNotBlank(areaCode)) {
					Area area = new Area();
					area.setCode(areaCode);
					if (dao.isExists(area) == false) {
						log.warn("没有找到商户编码为 " + merchant.getCode() + " 名称为 " + merchant.getFullName() + " 的所在地区 -> "
								+ areaCode);
						continue;
					}
					area = dictionaryManager.getDictionaryPo(areaCode, Area.class);
					merchant.setArea(area);
				}
				dao.baseSaveObject(merchant);
			}
		}
	}

	@Override
	public void parkingPlaceListReceive() {
		List<ParkingPlace> ps = dao.baseLists(new ParkingPlace(), false);
		String parkingPlaceCodes = "";
		for (ParkingPlace parkingPlace : ps) {
			parkingPlaceCodes += parkingPlace.getCode() + ",";
		}
		CurbParkingReq req = new CurbParkingReq();
		req.setService(ServiceConstants.CURB_TO_YUN_RECEIVE_PARKINGPLACE_LIST);
		req.setCharset("UTF-8");
		req.setVersion("V1.0");
		req.setSequence(TokenProcessor.getInstance().generateToken());
		req.setTimestamp((System.currentTimeMillis() / 1000) + "");
		req.setSignType("MD5");

		Map<String, String> maps = new HashMap<String, String>();
		maps.put("parkingPlaceCodes", parkingPlaceCodes);
		req.setReqData(JsonUtil.toJson(maps));
		CurbParkingRes res = sendMessage(req);
		if (res != null) {
			List<Map<String, Object>> rs = JsonUtil.toObject(res.getResData(), List.class);
			log.debug("从后台获取停车区数量为 " + rs.size());
			for (Map<String, Object> map : rs) {
				ParkingPlace parkingPlace = new ParkingPlace();
				parkingPlace.setCode((String) map.get("code"));
				parkingPlace.setAddress((String) map.get("address"));
				parkingPlace.setFullName((String) map.get("fullName"));
				parkingPlace.setShortName((String) map.get("shortName"));
				parkingPlace.setGlobalPath((String) map.get("globalPath"));
				parkingPlace.setLatitude((double) map.get("latitude"));
				parkingPlace.setLongitude((double) map.get("longitude"));
				parkingPlace.setPlatePrefix((String) map.get("platePrefix"));
				parkingPlace.setTotalFixedspaces((int) map.get("totalFixedspaces"));
				parkingPlace.setValid(true);
				String merchantCode = (String) map.get("merchantCode");
				String areaCode = (String) map.get("areaCode");
				String typeCode = (String) map.get("typeCode");

				if (StringUtil.isNotBlank(merchantCode)) {
					Merchant merchant = new Merchant();
					merchant.setCode(merchantCode);
					if (dao.isExists(merchant) == false) {
						log.warn("没有找到商户编码为 " + parkingPlace.getCode() + " 名称为 " + parkingPlace.getFullName()
								+ " 的上级商户 -> " + merchantCode);
						continue;
					}
					merchant = (Merchant) dao.baseLists(merchant, false).get(0);
					parkingPlace.setParentGroup(merchant);
					parkingPlace.setMerchant(merchant);
				}
				if (StringUtil.isNotBlank(areaCode)) {
					Area area = new Area();
					area.setCode(areaCode);
					if (dao.isExists(area) == false) {
						log.warn("没有找到商户编码为 " + parkingPlace.getCode() + " 名称为 " + parkingPlace.getFullName()
								+ " 的所在地区 -> " + areaCode);
						continue;
					}
					area = dictionaryManager.getDictionaryPo(areaCode, Area.class);
					parkingPlace.setArea(area);
				}
				if (StringUtil.isNotBlank(typeCode)) {
					ParkingPlaceType parkingPlaceType = new ParkingPlaceType();
					parkingPlaceType.setCode(typeCode);
					if (dao.isExists(parkingPlaceType) == false) {
						log.warn("没有找到商户编码为 " + parkingPlace.getCode() + " 名称为 " + parkingPlace.getFullName()
								+ " 的类型 -> " + typeCode);
						continue;
					}
					parkingPlaceType = dictionaryManager.getDictionaryPo(areaCode, ParkingPlaceType.class);
					parkingPlace.setType(parkingPlaceType);
				}
				dao.baseSaveObject(parkingPlace);
			}
		}
	}

	@Override
	public void employeeListReceive() {
		List<Employee> es = dao.baseLists(new Employee(), false);
		String employeeCodes = "";
		for (Employee employee : es) {
			employeeCodes += employee.getCode() + ",";
		}
		CurbParkingReq req = new CurbParkingReq();
		req.setService(ServiceConstants.CURB_TO_YUN_RECEIVE_EMPLOYEE_LIST);
		req.setCharset("UTF-8");
		req.setVersion("V1.0");
		req.setSequence(TokenProcessor.getInstance().generateToken());
		req.setTimestamp((System.currentTimeMillis() / 1000) + "");
		req.setSignType("MD5");

		Map<String, String> maps = new HashMap<String, String>();
		maps.put("employeeCodes", employeeCodes);
		req.setReqData(JsonUtil.toJson(maps));
		CurbParkingRes res = sendMessage(req);
		if (res != null) {
			List<Map<String, Object>> rs = JsonUtil.toObject(res.getResData(), List.class);
			log.debug("从后台获取用户数量为 " + rs.size());
			for (Map<String, Object> map : rs) {
				Employee employee = new Employee();
				employee.setCode((String) map.get("code"));
				String joinDateStr = (String) map.get("joinDate");
				if (StringUtil.isNotBlank(joinDateStr)) {
					employee.setJoinDate(DateUtil.convertStringToDate("yyyy-MM-dd", joinDateStr));
				}
				employee.setMobile((String) map.get("mobile"));
				employee.setName((String) map.get("name"));
				employee.setPassword((String) map.get("password"));
				employee.setValid(true);
				String groupCode = (String) map.get("groupCode");

				if (StringUtil.isNotBlank(groupCode)) {
					Group group = new Group();
					group.setCode(groupCode);
					if (dao.isExists(group) == false) {
						log.warn("没有找到用户编码为 " + employee.getCode() + " 名称为 " + employee.getName() + " 的上级商户 -> "
								+ groupCode);
						continue;
					}
					group = (Group) dao.baseLists(group, false).get(0);
					employee.setGroup(group);
					if(employee.getGroup().getCode().equals("ddzj")) {
						PermissionRole role = new PermissionRole();
						role.setCode("bk-manager");
						role = (PermissionRole) dao.baseLists(role, false).get(0);
						employee.getRoles().add(role);
					}
				}

				dao.baseSaveObject(employee);
			}
		}
	}

	@Override
	public void parkRecordPush() {
		// TODO Auto-generated method stub

	}

	private CurbParkingRes sendMessage(CurbParkingReq req) {
		Map<String, String> maps = new HashMap<String, String>();
		maps.put("service", req.getService());
		maps.put("timestamp", req.getTimestamp());
		maps.put("charset", req.getCharset());
		maps.put("version", req.getVersion());

		maps = ParamsProcessing.paraFilter(maps);
		String prestr = ParamsProcessing.createLinkStringByEncode(maps) + "&key=" + ServiceConstants.SERVICE_KEY;

		String sign = MD5Encrypt.getInstance().encrypt(prestr);
		req.setSign(sign);

		HttpUrlAndParams huap = new HttpUrlAndParams();
		huap.setUrlAddr(Constants.YUN_URL);

		huap.setMethod("POST");
		huap.setOutputStr(JsonUtil.toJson(req));
//		if(ServiceConstants.CURB_TO_YUN_RECEIVE_AREA_LIST.equals(req.getService())) {
//			huap.setSocketTimeout(100000);
//		}
//		if(ServiceConstants.CURB_TO_YUN_RECEIVE_EMPLOYEE_LIST.equals(req.getService())) {
//			huap.setSocketTimeout(120000);
//		}
		huap.setSocketTimeout(200000);
		HttpResult result = httpSend.postORGetSend(huap);
		if (result != null && result.isResult()) {
			CurbParkingRes res = JsonUtil.toObject(result.getContent(), CurbParkingRes.class);
			if (ServiceConstants.GW_RESULT_R001.equals(res.getRetCode())) {
				return res;
			}

		}
		log.warn("请求后台接口出现异常");
		return null;
	}

}
